package br.usjt.component.login.model;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.hibernate.envers.Audited;

@Audited
@Entity
@Table(name = "USER_TB", uniqueConstraints = { @UniqueConstraint(columnNames = "EMAIL"), @UniqueConstraint(columnNames = "CPF") })
@SequenceGenerator(name = "Usuario_Gen", sequenceName = "USER_SEQ", initialValue = 1, allocationSize = 50)
@NamedQueries({ @NamedQuery(name = Usuario.ALL, query = "SELECT u FROM Usuario u "),
        @NamedQuery(name = Usuario.TOTAL, query = "SELECT COUNT(u) FROM Usuario u") })
public class Usuario extends AbstractEntity<Integer> implements Serializable {

    public static final String ALL = "Usuario.populateUsers";
    public static final String TOTAL = "Usuario.countUsersTotal";

    private static final long serialVersionUID = 4821149961455645392L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "Usuario_Gen")
    @Column(name = "USER_ID", unique = true)
    private Integer id;

    @Column(name = "NAME", length = 80)
    private String nome;

    @Column(name = "LASTNAME", length = 100)
    private String sobrenome;

    @Column(name = "EMAIL", length = 100)
    private String email;

    @Column(name = "ADDRESS")
    private String endereco;

    @Column(name = "CELL_PHONE")
    private String telefoneCelular;

    @Column(name = "BUSINESS_PHONE")
    private String telefoneComercial;

    @Column(unique = true, length = 11, name = "CPF")
    private String cpf;

    @Column(name = "RG", length = 15)
    private String rg;

    @Column(name = "CEP")
    private String cep;

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "LOGIN_ID_FK", unique = true, nullable = true, insertable = true, updatable = true)
    private Login login;

    @Column(name = "STATUS", length = 1, nullable = false)
    private Boolean status = false;

    public Integer getId() {
        return id;
    }

    public String getNome() {
        return nome;
    }

    public String getSobrenome() {
        return sobrenome;
    }

    public String getEmail() {
        return email;
    }

    public String getEndereco() {
        return endereco;
    }

    public String getTelefoneCelular() {
        return telefoneCelular;
    }

    public String getTelefoneComercial() {
        return telefoneComercial;
    }

    public Boolean isStatus() {
        return status;
    }

    public String getCpf() {
        return cpf;
    }

    public String getRg() {
        return rg;
    }

    public String getCep() {
        return cep;
    }

    public Login getLogin() {
        return login;
    }

    public Boolean getStatus() {
        return status;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public void setSobrenome(String sobrenome) {
        this.sobrenome = sobrenome;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setEndereco(String endereco) {
        this.endereco = endereco;
    }

    public void setTelefoneCelular(String telefoneCelular) {
        this.telefoneCelular = telefoneCelular;
    }

    public void setTelefoneComercial(String telefoneComercial) {
        this.telefoneComercial = telefoneComercial;
    }

    public void setCpf(String cpf) {
        this.cpf = cpf;
    }

    public void setRg(String rg) {
        this.rg = rg;
    }

    public void setCep(String cep) {
        this.cep = cep;
    }

    public void setLogin(Login login) {
        this.login = login;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = super.hashCode();
        result = prime * result + ((cep == null) ? 0 : cep.hashCode());
        result = prime * result + ((cpf == null) ? 0 : cpf.hashCode());
        result = prime * result + ((email == null) ? 0 : email.hashCode());
        result = prime * result + ((endereco == null) ? 0 : endereco.hashCode());
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((login == null) ? 0 : login.hashCode());
        result = prime * result + ((nome == null) ? 0 : nome.hashCode());
        result = prime * result + ((rg == null) ? 0 : rg.hashCode());
        result = prime * result + ((sobrenome == null) ? 0 : sobrenome.hashCode());
        result = prime * result + ((status == null) ? 0 : status.hashCode());
        result = prime * result + ((telefoneCelular == null) ? 0 : telefoneCelular.hashCode());
        result = prime * result + ((telefoneComercial == null) ? 0 : telefoneComercial.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!super.equals(obj)) {
            return false;
        }
        if (!(obj instanceof Usuario)) {
            return false;
        }
        Usuario other = (Usuario) obj;
        if (cep == null) {
            if (other.cep != null) {
                return false;
            }
        } else if (!cep.equals(other.cep)) {
            return false;
        }
        if (cpf == null) {
            if (other.cpf != null) {
                return false;
            }
        } else if (!cpf.equals(other.cpf)) {
            return false;
        }
        if (email == null) {
            if (other.email != null) {
                return false;
            }
        } else if (!email.equals(other.email)) {
            return false;
        }
        if (endereco == null) {
            if (other.endereco != null) {
                return false;
            }
        } else if (!endereco.equals(other.endereco)) {
            return false;
        }
        if (id == null) {
            if (other.id != null) {
                return false;
            }
        } else if (!id.equals(other.id)) {
            return false;
        }
        if (login == null) {
            if (other.login != null) {
                return false;
            }
        } else if (!login.equals(other.login)) {
            return false;
        }
        if (nome == null) {
            if (other.nome != null) {
                return false;
            }
        } else if (!nome.equals(other.nome)) {
            return false;
        }
        if (rg == null) {
            if (other.rg != null) {
                return false;
            }
        } else if (!rg.equals(other.rg)) {
            return false;
        }
        if (sobrenome == null) {
            if (other.sobrenome != null) {
                return false;
            }
        } else if (!sobrenome.equals(other.sobrenome)) {
            return false;
        }
        if (status == null) {
            if (other.status != null) {
                return false;
            }
        } else if (!status.equals(other.status)) {
            return false;
        }
        if (telefoneCelular == null) {
            if (other.telefoneCelular != null) {
                return false;
            }
        } else if (!telefoneCelular.equals(other.telefoneCelular)) {
            return false;
        }
        if (telefoneComercial == null) {
            if (other.telefoneComercial != null) {
                return false;
            }
        } else if (!telefoneComercial.equals(other.telefoneComercial)) {
            return false;
        }
        return true;
    }

}
